#include "File/SystemPath.h"
#include "Functions/File.h"
#include "Functions/String.h"
#include "Functions/TerminalIO.h"
#include "TSystem.h"
using namespace std;
using namespace BOSS_Afterburner::File;
using namespace BOSS_Afterburner::TerminalIO;

SystemPath::SystemPath(const TString& path)
{
  SetPath(path);
}

void SystemPath::SetPath(const TString& path)
{
  TString absPath = GetAbsolutePath(DirName(path));
  if(absPath.EqualTo(""))
  {
    PrintWarning(Form("Path \"%s\" does not exist", path.Data()));
    return;
  }
  fPWD  = absPath;
  fName = BaseName(path);
  fSystemPath.SetNameTitle(GetPath(), GetPath());
}

const char* SystemPath::GetPath() const
{
  if(IsEmpty()) return "";
  if(fPWD.EqualTo(".")) return fName.Data();
  if(fName.EqualTo(".")) return fPWD.Data();
  return Form("%s/%s", fPWD.Data(), fName.Data());
}

Bool_t SystemPath::IsEmpty() const
{
  return fPWD.EqualTo("") && fName.EndsWith("");
}